/*
 * Copyright (c) 2024 iSOFT INFRASTRUCTURE SOFTWARE CO., LTD.
 * easyAda is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */

#include <core/notification.h>
#include <core/schedule.h>
#include <caps/notification.h>
#include <uapi/errors.h>
#include <uapi/ipcops.h>

static inline void cap_ntfn_set_obj(capability_t *cap, notification_t *obj)
{
    cap_set_long_info(cap, (unsigned long)obj);
}

void cap_ntfn_init(capability_t *cap, notification_t *obj)
{
    notification_get(obj);
    cap_basic_init(cap, CAP_NTFN);
    cap_ntfn_set_obj(cap, obj);
}

void cap_ntfn_dispatcher(capability_t *cap, unsigned long tag, unsigned long m0, unsigned long m1, unsigned long m2,
                         unsigned long m3, unsigned long m4, unsigned long m5)
{
    notification_t *ntfn = cap_ntfn_get_obj(cap);

    switch (msgtag_get_op(tag)) {
        case CAP_NTFN_OP_WAIT:
            notification_wait(ntfn, current(), msgtag_get_extra(tag) == 0);
            break;

        case CAP_NTFN_OP_SIGNAL:
            notification_signal(ntfn, current());
            break;

        case CAP_NTFN_OP_CANCEL:
            notification_cancel(ntfn);
            notification_put(ntfn);
            break;

        default:
            task_set_retcode(current(), ERR_TO_RETCODE(-ENOSYS));
            break;
    }
}
